﻿using Max.PH4_2102A.Shopping.Domain;
using Max.PH4_2102A.Shopping.IRespository;
using Max.PH4_2102A.Shopping.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.PH4_2102A.Shopping.Service
{
    /// <summary>
    /// 品牌的服务实现
    /// </summary>
    public class BrandService : IBrandService
    {
        IBrandRespository _respository;
        /// <summary>
        /// 构造函数注入品牌仓储接口
        /// </summary>
        /// <param name="respository"></param>
        public BrandService(IBrandRespository respository)
        {
            _respository = respository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Delete(BrandModel model)
        {
            return _respository.Delete(model);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="brands"></param>
        /// <returns></returns>
        public int Delete(List<BrandModel> models)
        {
            return _respository.Delete(models);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IQueryable<BrandModel> GetAll()
        {
            return _respository.Query();
        }

        /// <summary>
        /// 根据名称查询
        /// </summary>
        /// <param name="brandName"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IQueryable<BrandModel> GetAll(string brandName)
        {
            //因为这是服务层：逻辑处理。所以，所有的数据都来自于仓储层
            var list = _respository.Query();

            //再根据具体的条件进行查询
            list = list.Where(x => x.Name.Contains(brandName ?? ""));

            //返回查询结果
            return list;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="brandName"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IQueryable<BrandModel> GetAll(string brandName, int pageIndex, int pageSize, out int total)
        {
            var list = _respository.Query(); //所有数据 -- 设：100条
            list = list.Where(x => x.Name.Contains(brandName ?? "")); //根据条件去查 --- 设：查'张姓'，得结果5条

            total = list.Count(); //符合条件的总记录条数  ---- 结果的5条放到total

            list = list.Skip((pageIndex-1)*pageSize).Take(pageSize); //对查询条件进行分页   ---对5条数据进行分页

            return list;  //返回分页后的结果数据
        }

        /// <summary>
        /// 查询、排序和分页
        /// </summary>
        /// <param name="brandName"></param>
        /// <param name="isAsc">(默认根据名称来排序)</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IQueryable<BrandModel> GetAll(string brandName, bool isAsc, int pageIndex, int pageSize, out int total)
        {
            var list = _respository.Query();

            list = list.Where(x => x.Name.Contains(brandName ?? ""));

            //根据名称进行升和降排序
            if(isAsc)
                list = list.OrderBy(x => x.Name);
            else
                list = list.OrderByDescending(x => x.Name);

            total = list.Count();

            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize);

            return list;

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int Insert(BrandModel model)
        {
            int result = 1;
            //应该是要判断，品牌名称是否存在
            var m = _respository.Query().Where(x => x.Name.ToLower().Equals(model.Name.ToLower())).FirstOrDefault();

            if (m == null)
                _respository.Insert(model);
            else
            {
                //表示品牌名称已存在
                result = -1;
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(BrandModel model)
        {
            return _respository.Update(model);
        }
    }
}
